package edu.pitt.cs.cs2510.project3.model;

import java.util.ArrayList;
import java.util.List;

public class File implements Cloneable {
	//public double hotness;
	public int fileId;
	public int size;
	public int timeInTransit = 0;
	public int numBlocks;
	public boolean master;
	public WorkerServer workerServer;
	
	final List<Integer> AccessTimes = new ArrayList<Integer>();
	
	public File(int fileId, int size, double hotness, boolean complete, boolean master)
	{
		this.fileId = fileId;
		//this.hotness = hotness;
		this.size = size;
		if (complete)
		{
			this.fillBlocks();
		}
		workerServer = null;
	}

	public File(int fileId, int size, double hotness, boolean master)
	{
		this(fileId, size, hotness, true, master);
	}

	public File(int fileId, int size, double hotness, int numBlocks) {
		this.fileId = fileId;
		//this.hotness = hotness;
		this.size = size;
		this.numBlocks = numBlocks;
	}

	@Override
	public String toString() {
		return "File [fileId=" + fileId + ", size="
				+ size + ", timeInTransit=" + timeInTransit
				+ ", numBlocks=" + numBlocks + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		File other = (File) obj;
		if (fileId != other.fileId)
			return false;
		return true;
	}
	
	public void fillBlocks() {
		this.numBlocks = this.size;
	}

	@Override
	public File clone()
	{
		File file = new File(this.fileId, this.size, 0, this.numBlocks);
		return file;
	}
	
	public void touch(int accessTime) {
		AccessTimes.add(accessTime);
	}
	
	public float getHotness(int currentTime, int withinInterval) {
		int i=0;
		for(i=AccessTimes.size()-1; i>=0; i--) //all accesses should be in order of time
			if(AccessTimes.get(i).intValue() < (currentTime - withinInterval))
				break; //should be no more accesses... i think.
		float divisor = withinInterval;
		if(AccessTimes.size() > 0)
			divisor = Math.min(currentTime - AccessTimes.get(0).intValue(), withinInterval);
		return ((AccessTimes.size() - i)-1) / divisor;
	}	
}